rm(list=ls())
gc()
## SURVEY RESULT TABLES
library(modelsummary)
library(dplyr)
library(tidyr)
library(stringr)
library(tibble)
library(readr)
library(gt)
library(data.table)
library(gt)
#gc()

load('results/survey-results-no-weights.Rdata')
load('results/survey-results-weights.Rdata')


source('felm-summary-table.R')



vars = c('Dem Exp', 'HH Dem', 'Dem Exp * Dem', 'Rep Exp', 'HH Rep' ,'Rep Exp * Rep',
         'BG White', 'BG Age', 'BG Registered', 'BG Med. HH Income', 'BG College', 'BG Homeowner', 'BG Year Built',
         'BG Drive Work', 'BG Unemployed', 'BG Med. House Value', 'Asian','Black', 'Hispanic', 'White', 'Age','Male','Liberalism', 'Married'
, 'College', 'Years Residence','Party7'        )
names(vars)= c('DemSpExp_nohh', 'hh.d.adj','DemSpExp_nohh:Democrat','RepSpExp_nohh','hh.r.adj', 'RepSpExp_nohh:Republican' ,
               'WhiteBlockGroup', 'AgeBlockGroup', 'RegsBlockGroup', 'HHIncomeBlockGroup', 'CollegeBlockGroup', 'HomeownerBlockGroup',
               'YearBuilBlockGroup', 'DriveWorkBlockGroup', 'EmplBlockGroup','HouseValueBlockGroup', 'RaceAsian','RaceBlack','RaceHispanic','RaceWhite',
               'Age','Male','Liberalism','Married','College','YearsResidence','Party7')
l = list(w1DemSpExp, m1DemSpExp,
         w1RepSpExp, m1RepSpExp,
         w2DemSpExp, m2DemSpExp,
         w2RepSpExp, m2RepSpExp,
         w5DemSpExp, m5DemSpExp,
         w5RepSpExp, m5RepSpExp)
names(l) = c('(1)', '(2)','(3)', '(4)', '(5)', '(6)', '(7)', '(8)','(9)', '(10)', '(11)', '(12)')
tab1 = felm.summary.table(summaries = l,
                          coef_map = vars, output ='gt',
                          model.names=names(l), fmt =2)

tab1 %>%
  
  # column labels
  tab_spanner(label = 'Neighbor PID', columns = 2:5) %>%
  tab_spanner(label = 'Contact Dems', columns = 6:7) %>%
  tab_spanner(label = 'Contact Reps', columns = 8:9) %>%
  tab_spanner(label = 'Comfort', columns = 10:13) %>%
  
  
  as_latex()%>%
  as.character %>%
  str_replace_all('longtable','tabular')%>%
  str_replace_all(as.character(m1DemSpExp$N),format(m1DemSpExp$N, trim=T, big.mark=','))%>%
  str_replace_all(as.character(w1DemSpExp$N),format(w1DemSpExp$N, trim=T, big.mark=','))%>%
  str_replace_all(as.character(m1RepSpExp$N),format(m1RepSpExp$N, trim=T, big.mark=','))%>%
  str_replace_all(as.character(w1RepSpExp$N),format(w1RepSpExp$N, trim=T, big.mark=','))%>%
  str_replace_all(as.character(m2RepSpExp$N),format(m2RepSpExp$N, trim=T, big.mark=','))%>%
  str_replace_all(as.character(m2DemSpExp$N),format(m2DemSpExp$N, trim=T, big.mark=','))%>%
  str_replace_all(as.character(m5RepSpExp$N),format(m5RepSpExp$N, trim=T, big.mark=','))%>%
  str_replace_all(as.character(m5DemSpExp$N),format(m5DemSpExp$N, trim=T, big.mark=','))%>%
  str_replace_all(as.character(w2RepSpExp$N),format(w2RepSpExp$N, trim=T, big.mark=','))%>%
  str_replace_all(as.character(w2DemSpExp$N),format(w2DemSpExp$N, trim=T, big.mark=','))%>%
  str_replace_all(as.character(w5RepSpExp$N),format(w5RepSpExp$N, trim=T, big.mark=','))%>%
  str_replace_all(as.character(w5DemSpExp$N),format(w5DemSpExp$N, trim=T, big.mark=','))%>%
  
  write_file('tables/TabS15.tex')



### comfort



vars = c('Comfort', 'Asian','Black', 'Hispanic', 'White', 'Age','Male', 'Married'
         , 'College', 'Years Residence'       )
names(vars)= c('Comfort', 'RaceAsian','RaceBlack','RaceHispanic','RaceWhite',
               'Age','Male','Married','College','YearsResidence')
l = list(w6DemSpExpDems, w6DemSpExpReps, w6DemSpExpOths,
         w6RepSpExpDems, w6RepSpExpReps, w6RepSpExpOths)
names(l) = c('(1)', '(2)','(3)', '(4)', '(5)', '(6)')
tab1 = felm.summary.table(summaries = l,
                          coef_map = vars, output ='gt',
                          model.names=names(l), fmt =3)

tab1 %>%
  
  # column labels
  tab_spanner(label = '2020 Democrat', columns = 2:4) %>%
  tab_spanner(label = '2020 Republican', columns = 5:7) %>%
  
  
  as_latex()%>%
  as.character %>%
  str_replace_all('longtable','tabular')%>%
  str_replace_all(as.character(w6DemSpExpDems$N),format(w6DemSpExpDems$N, trim=T, big.mark=','))%>%
  str_replace_all(as.character(w6DemSpExpReps$N),format(w6DemSpExpReps$N, trim=T, big.mark=','))%>%
  str_replace_all(as.character(w6DemSpExpOths$N),format(w6DemSpExpOths$N, trim=T, big.mark=','))%>%
  str_replace_all(as.character(w6RepSpExpDems$N),format(w6RepSpExpDems$N, trim=T, big.mark=','))%>%
  str_replace_all(as.character(w6RepSpExpReps$N),format(w6RepSpExpReps$N, trim=T, big.mark=','))%>%
  str_replace_all(as.character(w6RepSpExpOths$N),format(w6RepSpExpOths$N, trim=T, big.mark=','))%>%
  
  write_file('tables/TabS16.tex')




vars = c('Comfort', 'Asian','Black', 'Hispanic', 'White', 'Age','Male', 'Married'
         , 'College', 'Years Residence'       )
names(vars)= c('Comfort', 'RaceAsian','RaceBlack','RaceHispanic','RaceWhite',
               'Age','Male','Married','College','YearsResidence')
l = list(m6DemSpExpDems, m6DemSpExpReps, m6DemSpExpOths,
         m6RepSpExpDems, m6RepSpExpReps, m6RepSpExpOths)
names(l) = c('(1)', '(2)','(3)', '(4)', '(5)', '(6)')
tab1 = felm.summary.table(summaries = l,
                          coef_map = vars, output ='gt',
                          model.names=names(l), fmt =3)

tab1 %>%
  
  # column labels
  tab_spanner(label = '2020 Democrat', columns = 2:4) %>%
  tab_spanner(label = '2020 Republican', columns = 5:7) %>%
  
  
  as_latex()%>%
  as.character %>%
  str_replace_all('longtable','tabular')%>%
  str_replace_all(as.character(m6DemSpExpDems$N),format(m6DemSpExpDems$N, trim=T, big.mark=','))%>%
  str_replace_all(as.character(m6DemSpExpReps$N),format(m6DemSpExpReps$N, trim=T, big.mark=','))%>%
  str_replace_all(as.character(m6DemSpExpOths$N),format(m6DemSpExpOths$N, trim=T, big.mark=','))%>%
  str_replace_all(as.character(m6RepSpExpDems$N),format(m6RepSpExpDems$N, trim=T, big.mark=','))%>%
  str_replace_all(as.character(m6RepSpExpReps$N),format(m6RepSpExpReps$N, trim=T, big.mark=','))%>%
  str_replace_all(as.character(m6RepSpExpOths$N),format(m6RepSpExpOths$N, trim=T, big.mark=','))%>%
  
  write_file('tables/TabS17.tex')

